export function reBuild(rows) {
    let workstations = getAllWorkstation(rows)
    return {
        columns: buildColumns(workstations),
        rows: buildRows(rows)
    }
}

function buildRows(rows) {
    rows.forEach(record => {
        let workstation = record.workstation
        let processParameterList = toObj(record.processParameter)
        if (processParameterList == null) {
            return
        }
        record[workstation + 'beatTime'] = record.beatTime
        record[workstation + 'duration'] = record.duration
        record[workstation + 'processingResult'] = record.processingResult
        record[workstation + 'processingTime'] = record.processingTime

        processParameterList.forEach(parameter => {
            Object.keys(parameter).forEach(key => {
                record[workstation + processKey(key)] = parameter[key]
            })
        })
    });
    return rows
}


function buildColumns(workstations) {
    let columns = []
    Object.keys(workstations).forEach(workstation => {
        let params = workstations[workstation]
        columns.push({
            label: workstation,
            prop: workstation,
            width: calculateWidth(workstation),
            children: buildChildren(params)
        })
    })
    return columns
}

function buildChildren(params) {
    let list = []
    params.forEach(param => {
        list.push({
            dist: param.dist,
            label: param.label,
            prop: param.prop,
            width: calculateWidth(param.label)
        })
    })
    return list
}

function getAllWorkstation(rows) {
    let workstations = {}
    rows.forEach(record => {
        let workstation = record.workstation
        let paramsList = workstations[workstation]
        if (!paramsList) {
            paramsList = []
        }
        let processParameterList = toObj(record.processParameter)
        if (processParameterList == null) {
            processParameterList = []
        }
        processParameterList.forEach(parameter => {
            Object.keys(parameter).forEach(key => {
                let prop = processKey(workstation + key)
                if (!isExist(paramsList, prop)) {
                    paramsList.push({ prop: prop, label: key })
                }
            })
        })
        workstations[workstation] = paramsList
    });
    Object.keys(workstations).forEach(key => {
        let paramsList = workstations[key]
        paramsList.unshift({ prop: key + 'beatTime', label: '上线时间' })
        paramsList.push({ prop: key + 'processingTime', label: '下线时间' })
        paramsList.push({ prop: key + 'duration', label: '节拍/秒' })
        paramsList.push({ prop: key + 'processingResult', label: '工序结果', dist: 'processing_result' })
        workstations[key] = paramsList
    })
    return workstations
}

function isExist(list, prop) {
    for (let i = 0; i < list.length; i++) {
        if (list[i].prop == prop) {
            console.log(prop)
            return true
        }
    }
    return false
}

function processKey(key) {
    return key.replace(":", "")
}

function toObj(json) {
    if (json == null || json == "") {
        return null;
    }
    return JSON.parse(json)
}


function calculateWidth(key) {
    if (key.indexOf('时间') != -1) {
        return 180;
    }
    return key.length * 20
}
